<?php

namespace app\common\command;

use app\common\classes\BehaviorRedis;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\Db;

class LinkDataReport extends Command
{
    protected function configure()
    {
        $this->setName('link_data_report')
            ->addArgument('action', Argument::OPTIONAL, "方法名称")
            ->addOption('date', null, Option::VALUE_REQUIRED, '任务数据日期')
            ->setDescription('链接维度任务');
    }

    protected function execute(Input $input, Output $output)
    {

        $action = $input->getArgument('action');

        if (method_exists($this, $action)) {

            $date = $input->hasOption('date') ? $input->getOption('date') : date('Y-m-d', strtotime('-1days'));

            $this->$action($date);
        }

    }

    /*链接每日数据*/
    public function linkData($date)
    {
        $list = Db::table("quick_link")->where('is_delete','<',2)
            ->where("create_time", "<=", "{$date} 23:59:59")->column('id');
        $redis = new BehaviorRedis();

        foreach ($list as $k => $v) {
            $data = [];
            $data['link_id'] = $v;
            $data['report_date'] = $date;
            $data['new_fan_num'] = Db::table("quick_user")->where("regdate", $date)->where("source_link_id", $v)->count();//新粉数
            $data['jiazhuo_num'] = Db::table("quick_user_add_shortcut")->where("create_date", $date)->where("link_id", $v)->count();//加桌数
            $data['recharge_money'] = Db::table("pay_order")->where("create_date", $date)->where("link_id", $v)->where("status", 2)->sum('total_fee') ?? 0;//充值金额
            $data['recharge_count'] = Db::table("pay_order")->where("create_date", $date)->where("link_id", $v)->where("status", 2)->count();//充值次数
            $data['lift_count'] = Db::table("pay_order")->where("create_date", $date)->where("link_id", $v)->count();//起吊次数
            $data['lift_uv'] = Db::table("pay_order")->where("create_date", $date)->where("link_id", $v)->group('uid')->count();//起吊人数
            $data['pay_uv'] = Db::table("pay_order")->where("create_date", $date)->where("link_id", $v)->where("status", 2)->group('uid')->count();//充值人数
            $data['click_uv'] = $redis->getLinkUvCount($v, $date);//点击人数

            $data['new_user_money'] = Db::table("pay_order")->where("create_date", $date)->where("link_id", $v)->where("status", 2)->where("user_reg_date", $date)->sum('total_fee') ?? 0;//新用户充值
            $data['new_pay_num'] = Db::table("pay_order")->where("create_date", $date)->where("link_id", $v)->where("status", 2)->where("user_reg_date", $date)->group('uid')->count();//新用户充值人数
            $data['new_pay_count'] = Db::table("pay_order")->where("create_date", $date)->where("link_id", $v)->where("status", 2)->where("user_reg_date", $date)->count();//新用户充值笔数
            $data['new_gs_money'] = Db::table("pay_order")->where("create_date", $date)->where("link_id", $v)->where("status", 2)->where("user_source_date", $date)->sum('total_fee') ?? 0;//新归属用户充值

            if ($data['new_fan_num'] || $data['jiazhuo_num'] || $data['recharge_money'] || $data['recharge_count'] || $data['lift_count'] || $data['pay_uv'] || $data['click_uv'] || $data['new_user_money']
                || $data['new_pay_num'] || $data['new_pay_count'] || $data['new_gs_money']) {
                $daily = Db::table("rp_link_daily")->where("link_id", $v)->where("report_date", $date)->find();
                if (!$daily) {
                    Db::table("rp_link_daily")->insert($data);
                } else {
                    unset($data['click_uv']);//防止redis过期，重跑任务被赋0
                    Db::table("rp_link_daily")->where("id", $daily['id'])->update($data);
                }
            }
        }
    }


}